﻿// ===================================
// <copyright>LogoUI Co.</copyright>
// <author>Vlad Spivak</author>
// <email>mailto:vlads@logoui.co.il</email>
// <created>21/00/10</created>
// <lastedit>Tuesday, November 23, 2010</lastedit>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
// <remarks>Part of this software based on various internet sources, mostly on the works
// of members of Wpf Disciples group http://wpfdisciples.wordpress.com/
// Also project may contain code from the frameworks: 
//        Nito 
//        OpenLightGroup
//        nRoute
// </remarks>
// ====================================================================================//


//note:this stuff does nothing in WPF,preserved for xaml compatibility

using System;
using System.Threading;
using System.Windows;

namespace LogoFX.Infra.Interactivity
{
    /// <summary>
    /// Represents Mesurement behavior extension in form of attached properties.
    /// </summary>
    [Obsolete("Use LogoFX.Infr.aMeasurementBehaviour if possible")]
    public class Measurement : BehaviorBase<Measurement>
    {


        void OnLayoutUpdated(object sender, EventArgs e)
        {
            Owner.Dispatcher.BeginInvoke(new ThreadStart(delegate
            {
                SetActualWidth(Owner, Owner.ActualWidth);
                SetActualHeight(Owner, Owner.ActualHeight);
            }));
        }

        /// <summary>
        /// Detaches the instance of the object from <see cref="DependencyObject"/> and its event handlers.
        /// </summary>
        protected override void OnAttach()
        {
#if SILVERLIGHT
            Owner.LayoutUpdated += OnLayoutUpdated;
#endif
            base.OnAttach();
        }
        protected override void OnDetach()
        {
#if SILVERLIGHT
            Owner.LayoutUpdated -= OnLayoutUpdated;
#endif
            base.OnDetach();
        }


        /// <summary>
        /// Gets <see cref="CalculateRealSizesProperty"/> value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>CalculateRealSizes</c> propery.</param>
        /// <returns><c>CalculateRealSizes</c> property value.</returns>
        public static bool GetCalculateRealSizes(DependencyObject obj)
        {
            return (bool)obj.GetValue(CalculateRealSizesProperty);
        }

        /// <summary>
        /// Sets <see cref="CalculateRealSizesProperty"/> value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>CalculateRealSizes</c> propery.</param>
        /// <param name="value">New <c>CalculateRealSizes</c> propery value.</param>
        public static void SetCalculateRealSizes(DependencyObject obj, bool value)
        {
            obj.SetValue(CalculateRealSizesProperty, value);
        }

        /// <summary>
        /// Defines the <c>CalculateRealSizes</c> dependency property.
        /// </summary>
        public static readonly DependencyProperty CalculateRealSizesProperty = DependencyProperty.RegisterAttached(
          "CalculateRealSizes",
          typeof(bool),
          typeof(Measurement),
          new PropertyMetadata(false, CalculateRealSizesChanged));

        /// <summary>
        /// The <see cref="PropertyChangedCallback"/> that is called when the <see cref="CalculateRealSizesProperty"/> is changed.
        /// </summary>
        /// <param name="d">An instance of <see cref="DependencyObject"/> object whose <see cref="CalculateRealSizesProperty"/> was changed.</param>
        /// <param name="e">An instance of the <see cref="DependencyPropertyChangedEventArgs"/> class.</param>
        private static void CalculateRealSizesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                GetOrAttach(d as FrameworkElement);
            }
            else
            {
                Measurement m = GetInstance(d);
                if (m != null)
                {
                    m.Detach();
                    //SetInstance(d, null);
                }
            }
        }

        ///////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets <see cref="ActualWidthProperty"/> value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>ActualWidth</c> propery.</param>
        /// <returns>Actual width property value.</returns>
        public static double GetActualWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(ActualWidthProperty);
        }

        /// <summary>
        /// Sets <see cref="ActualWidthProperty"/> value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>ActualWidth</c> propery.</param>
        /// <param name="value">New <c>ActualWidth</c> propery value.</param>
        public static void SetActualWidth(DependencyObject obj, double value)
        {
            obj.SetValue(ActualWidthProperty, value);
        }

        /// <summary>
        /// Defines the <c>ActualWidth</c> dependency property.
        /// </summary>
        public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.RegisterAttached(
          "ActualWidth",
          typeof(double),
          typeof(Measurement),
          new PropertyMetadata(0.0));

        ///////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets <see cref="ActualHeightProperty"/>  value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>ActualHeight</c> propery.</param>
        /// <returns>Actual height property value.</returns>
        public static double GetActualHeight(DependencyObject obj)
        {
            return (double)obj.GetValue(ActualHeightProperty);
        }

        /// <summary>
        /// Sets <see cref="ActualHeightProperty"/> value of a <see cref="DependencyObject"/>.
        /// </summary>
        /// <param name="obj">A dependency object, which contains <c>ActualHeight</c> propery.</param>
        /// <param name="value">New <c>ActualHeight</c> propery value.</param>
        public static void SetActualHeight(DependencyObject obj, double value)
        {
            obj.SetValue(ActualHeightProperty, value);
        }

        /// <summary>
        /// Defines the <c>ActualHeight</c> dependency property.
        /// </summary>
        public static readonly DependencyProperty ActualHeightProperty = DependencyProperty.RegisterAttached(
          "ActualHeight",
          typeof(double),
          typeof(Measurement),
          new PropertyMetadata(0.0));

        ///////////////////////////////////////////////////////////////////
    }
}
